package showtime.framework.ui
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;

	public class BitmapButton extends UIComponent
	{
		private var bitmapData:BitmapData = null;
		private const THRESHOLD:Number  = 0;
		private var clazz:Class = null;
		
		public function BitmapButton(clazz:Class, parent:DisplayObjectContainer, xpos:Number = 0, ypos:Number = 0)
		{
			this.clazz = clazz;
			super(parent, xpos, ypos);
			
		}
		
		override protected function initialize():void
		{
			super.initialize();
		}
		
		override protected function createChildren():void
		{
			var image:Bitmap = new clazz();
			addChild(image);
			
			bitmapData = image.bitmapData;
			
			addEventListener(MouseEvent.CLICK, clickHandler);
			addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		}
		
		private function mouseMoveHandler(evt:MouseEvent):void
		{
			var pixel:uint = bitmapData.getPixel32(mouseX, mouseY);
			useHandCursor = buttonMode = ((pixel >>>24) > THRESHOLD);
		}
		
		private function clickHandler(evt:MouseEvent):void
		{
			if (!useHandCursor)
			{
				evt.stopImmediatePropagation();
			}
		}
	}
	
	
}